package old.gui;

import java.util.HashMap;


import utilities.SimpleHashMap;

import com.jme3.asset.AssetManager;
import com.jme3.input.InputManager;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.scene.Node;
import com.jme3.system.AppSettings;

import eventManager.EventManager;
import eventManager.IEventData;
import eventManager.IEventListener;
import gameAppLayer.MainApp;

/*
 * Creates and initializes all GUIStates
 * At startup, loads all GUIStates and attaches primary to playerView
 * Receives messages to transition between GUIStates and shifts attachments to playerView
 */

/*
 * Given game events/states, selects appropriate state from VGUIList and attaches to playerView.  PlayerView is then attached
 * to the RootNode by the ViewSystem, allowing rendering 
 */

//VGUIList maintains a container of all GUI state and object views
//MGUIList maintains a list of all GUI state and object models
public class GUIViewManager implements IEventListener {
	
	public AssetManager assetManager;
	public AppSettings settings;
	public Node guiNode;
	public EventManager eventManager;
	public InputManager inputManager;
	public MainApp app;
	
	//Map that contains all the GUIStates currently loaded
	private SimpleHashMap<String, GUILayout> stateMap = new SimpleHashMap<String, GUILayout>();	
	
	public float settingAspectRatio;
	private String currentState; //Name of currently attached state.
	private String defaultState = "DEFAULT_MENU";
	
	public GUIViewManager(MainApp app)
	{
		this.app = app;
		//this.assetManager = app.getAssetManager();
		this.settings = app.getAppSettings();
		this.guiNode = app.getGuiNode();
		//this.inputManager = app.getInputManager();
		this.settingAspectRatio = (float)(settings.getWidth())/(settings.getHeight());
		
		registerInputs();
	}
	
	//Register general input actions with inputManager
	public void registerInputs() 
	{
		app.getInputManager().addMapping("LEFT_CLICK", new MouseButtonTrigger(0));
				
		//left click and hold
		//left click and drag
		//right click
		//right click and hold, etc.
	}
	
	//Note:  Currently loads all states available at once.  May want to add intelligence
	//and have it only load a subset of most necessary states.
	public boolean loadStates()
	{
		System.out.println("Loading Default State");
		addState("DEFAULT_MENU", new StateDefaultMenu(app));
		
		System.out.println("Loading Main Menu State");
		addState("MAIN_MENU", new StateMainMenu(app));
				
		System.out.println("Loading Second Menu State");
		addState("SECOND_MENU", new StateSecondMenu(app));
		//etc...
		
		
		
		
		//attach initial state
		attachState("MAIN_MENU");		
		
		return true;
	}
	
	
	
	//Removes current GUIState from guiNode, attaches new GUIState
	public void changeState(String newState)
	{
		detachState(currentState);
		attachState(newState);
	}
	
	//Detaches all GUIStates from guiNode
	public void shutdownGUI()
	{
		guiNode.detachAllChildren();
	}
	
	private void detachState(String name)
	{
		
		guiNode.detachChildNamed(name);
		getState(name).removeEvents();
		getState(name).removeListeners();
		this.currentState = defaultState;
	}
	
	//Attaches GUIState to guiNode
	private void attachState(String name)
	{
		guiNode.attachChild(getState(name));
		getState(name).registerEvents();
		getState(name).registerListeners();
		this.currentState = name;
	}
	
	public void addState(String name, GUILayout state)
	{stateMap.put(name, state);}
	
	public GUILayout getState(String name)
	{return stateMap.get(name);}
	
	public void removeState(String name)
	{stateMap.remove(name);}
	
	//Sends update command to currently active GUIState
	public void update()
	{		
		getState(currentState).update();		
	}
	
	//Note:  This is going to get long...
	public void handleEvent(IEventData event) {
		if (event.getEventName().equalsIgnoreCase("GUIEVENT_NEW_GAME")) {
			changeState("SECOND_MENU");
			System.out.println("Changed state");
		}
		else {
			System.out.println("Crap, GUIStateManager didn't handle it");
		}
		
	}
	
	
	
	

}
